@echo off

echo %~dp0

if "%1"=="" (
    set tradedate=%date:~0,4%%date:~5,2%%date:~8,2%
) else (
    set tradedate=%1
)

echo tradedate : %tradedate%

if not exist "%~dp0\%tradedate%.csv" (
    echo not found %tradedate%.csv
    goto end
)
if not exist "%~dp0\lastdate.xlsx" (
    echo not found lastdate.xlsx
    goto end
)
if not exist "%~dp0\csv-import.exe" (
    echo not found csv-import.exe
    goto end
)

csv-import.exe -last="%~dp0\lastdate.xlsx" -data="%~dp0\%tradedate%.csv"

echo opening lastdate.xlsx ...

start "" %~dp0\lastdate.xlsx"

echo waiting ...
ping -n 2 127.0.0.1>nul

echo closing excel ...
taskkill /f /im excel.exe

echo waiting ...
ping -n 3 127.0.0.1>nul
echo coping ...

copy /y "%~dp0\lastdate.xlsx" "%~dp0\import_%tradedate%.xlsx"

echo done.

:end
echo Press any key to continue!
pause>nul